package com.example.demo;

import com.jfinal.kit.Kv;
import java.util.List;
import java.util.concurrent.ExecutionException;

import lombok.extern.slf4j.Slf4j;
import org.noear.dami2.bus.Result;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.core.handle.UploadedFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class DemoController {
    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @Mapping("/hello")
    public String hello(@Param(defaultValue = "world") String name) throws ExecutionException, InterruptedException {
        EventUtils.listen( "hello",  (event, data, sink) -> {
                    log.info("hello event: {}", data);

                    //数据
                    data.set("name", name + ", hahaha");

                    //回调
                    sink.complete(Ret.create());
                });

//    EventUtils.send("hello", Kv.of("name", name));

        EventUtils.sendAndSubscribe("hello", Kv.by(name, name), ret -> {
            log.info("hello ret: {}", ret);
        });

//    Ret ret = EventUtils.call("hello", Kv.of("name", name));
//    log.info("result: {}", ret);

        return String.format("Hello %s!", name);
    }

    @Mapping("/upload")
    public void upload(@Param(value = "files") List<UploadedFile> files) {
        for (UploadedFile file : files) {
            System.out.println(file.getName());
        }
    }

    @Mapping("/upload2")
    public void upload2(@Param(value = "files") UploadedFile[] files) {
        for (UploadedFile file : files) {
            System.out.println(file.getName());
        }
    }
}
